import { useTitle } from "@vueuse/core";
import { Router } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

// modules/**/router/guard 需要默认导出的函数类型
export type RouterGuard = (router: Router) => void;

// 注册路由守卫
export function setupRouterGuard(router: Router) {
  // 改变标题
  changeTitle(router);
  // 导航进度条
  nprogress(router);
}

// 改变网页标题
function changeTitle(router: Router) {
  router.beforeEach((to) => {
    const title = to.meta.title;
    if (title) {
      useTitle(title);
    }
  });
}

// 进度条
function nprogress(router: Router) {
  NProgress.configure({
    showSpinner: true,
  });
  router.beforeEach(() => {
    NProgress.start();
  });
  router.afterEach(() => {
    NProgress.done();
  });
}
